//
//  YYEMineViewController.m
//  YuYingEDU
//
//  Created by 胡波 on 2017/12/27.
//  Copyright © 2017年 胡波. All rights reserved.
//

#import "YYEMineViewController.h"
#import "YYEPersonalTableViewCell.h"
#import "YYENoLoginTableViewCell.h"
// 登录
#import "YYELoginViewController.h"
// 课程收藏
#import "YYECourseCollectionViewController.h"
// 我的课程
#import "YYEMyCourseViewController.h"
// 我的习题
#import "YYEMyTopicViewController.h"
//// 我的钱包
//#import "YYEWalletViewController.h"
// 设置
#import "YYESettingViewController.h"
// 教师 我的课程
#import "YYETeacCourseViewController.h"
// 账户管理
#import "YYEAcountManaViewController.h"
// 我的心得
#import "YYELatestNotesViewController.h"

#import "YYELoginViewController.h"

// 错题本
#import "YYEWrongTopicViewController.h"

// 习题
#import "YYEExampleViewController.h"

// 我的问答
#import "YYETeacMyAnswerViewController.h"


@interface YYEMineViewController () <UITableViewDelegate,UITableViewDataSource,NoLoginDelegate,LogOutDelegate>

@property (nonatomic,strong) YYEBaseTableView *tableView;
@property (nonatomic,copy) NSString *type;


@property (nonatomic,strong) UIButton *headerBtn;
@property (nonatomic,strong) UILabel *nameLabel;

@property (nonatomic,assign) NSInteger number;


@end

@implementation YYEMineViewController

static NSString *personCell = @"personCell";
static NSString *nologinCell = @"nologinCell";
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self configUI];
    
    
    
}

- (void)getBadge {
    
    
    NSDictionary *dic = @{
                          @"uid":[LoginModel shareLogin].uid,
                          @"username":[LoginModel shareLogin].username,
                          @"token":[LoginModel shareLogin].token,
                          };
    [SVProgressHUD show];
    [AFNHelper post:Badge parameters:dic success:^(id responseObject) {
        
        //        NSLog(@"%@",responseObject);
        if ([responseObject[@"status"] isEqual:@1]) {
            
            NSNumber *countInvite = [responseObject objectForKey:@"data"];
            
            self.number = countInvite.integerValue;

            [self.tableView reloadData];
        }
        [SVProgressHUD showWithStatus:responseObject[@"message"]];
        [SVProgressHUD dismiss];
        
    } faliure:^(id error) {
        
        NSLog(@"请求失败");
        [SVProgressHUD dismiss];
        
    }];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    
    // 我的问答
    [[NSNotificationCenter defaultCenter] postNotificationName:@"KAnswerNotification" object:nil];
    
    self.type = GETDATAFROMUSERDEFAULT(@"role");
    [self.tableView reloadData];
    
    
    PersonModel *model = [InfoCache unarchiveObjectWithFile:Person];
    
    [self.headerBtn sd_setImageWithURL:[NSURL URLWithString:model.img] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"headerHolder"]];
    self.nameLabel.text = model.name;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];

    
    // 我的问答
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    
}

- (void)configUI {
    
    [self.view addSubview:self.tableView];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = GeneralBackColor;
    self.tableView.tableHeaderView = [self headerView];
    [self.tableView registerClass:[YYEPersonalTableViewCell class] forCellReuseIdentifier:personCell];
    [self.tableView registerClass:[YYENoLoginTableViewCell class] forCellReuseIdentifier:nologinCell];
    
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.bottom.equalTo(self.view.bottom);
    }];
    
}

- (UIView *)headerView {
    
    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH / 2)];
    
    UIImageView *backIV = [[UIImageView alloc]init];
    backIV.image = [UIImage imageNamed:@"headerBack"];
    [contentView addSubview:backIV];
    
    UIButton *headerBtn = [[UIButton alloc]init];
//    [headerBtn setImage:[UIImage imageNamed:@"headerHolder"] forState:UIControlStateNormal];
    [headerBtn addTarget:self action:@selector(acountManaClicked) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:headerBtn];
    self.headerBtn = headerBtn;
    
    UILabel *nameLabel = [[UILabel alloc]init];
    nameLabel.textColor = [UIColor blackColor];
    nameLabel.font = [UIFont systemFontOfSize:17];
//    nameLabel.text = @"陈晨";
    [contentView addSubview:nameLabel];
    self.nameLabel = nameLabel;
    
    
    [backIV makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(UIEdgeInsetsZero);
    }];
    
    [headerBtn makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(backIV.centerX);
        make.centerY.equalTo(backIV.centerY);
        make.size.equalTo(CGSizeMake(60, 60));
    }];
    headerBtn.layer.cornerRadius = 30;
    headerBtn.layer.masksToBounds = YES;
    headerBtn.layer.borderWidth = 3;
    headerBtn.layer.borderColor = [UIColor whiteColor].CGColor;

    [nameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(backIV.centerX);
        make.top.equalTo(headerBtn.bottom).offset(10);
    }];
    
    return contentView;
    
}

- (YYEBaseTableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[YYEBaseTableView alloc]initwithDelegate:self];
    }
    return _tableView;
    
}

#pragma mark -- UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if ([self.type isEqual:@1]) {
        return 1;
    }else if ([self.type isEqual:@0]) {
        return 2;
    }else {
        return 1;
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if ([self.type isEqual:@1]) {
        return 5;
    }else if ([self.type isEqual:@0]) {
        if (section == 0) {
            return 5;
        }else {
            return 1;
        }
    }else {
        return 1;
    }
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.type isEqual:@1]) {
        
        YYEPersonalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:personCell];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        NSArray *names = @[@"我的课程",@"我的问答",@"我的心得",@"数据统计",@"设置"];
        cell.nameLabel.text = names[indexPath.row];
        if (indexPath.row == ([tableView numberOfRowsInSection:indexPath.section] - 1)) {
            cell.backIV.image = [UIImage imageNamed:@"bottomBorder"];
            cell.lineView.hidden = YES;
        }else if (indexPath.row == 0) {
            cell.backIV.image = [UIImage imageNamed:@"topBorder"];
        }else {
            cell.backIV.image = [UIImage imageNamed:@"noBorder"];
        }
        
        if (indexPath.row == 1) {
            if (self.number > 0) {
                cell.redDot.hidden = NO;
                cell.redDot.text = [NSString stringWithFormat:@"%ld",self.number];
            }
            else {
                cell.redDot.hidden = YES;
                
            }
        }
        else {
            cell.redDot.hidden = YES;
            
        }
        
        return cell;
        
    }else if ([self.type isEqual:@0]) {
        
        YYEPersonalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:personCell];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.redDot.hidden = YES;

        if (indexPath.section == 0) {
            
            NSArray *names = @[@"课程收藏",@"我的课程",@"我的习题",@"我的心得",@"我的错题"];
            cell.nameLabel.text = names[indexPath.row];
            if (indexPath.row == ([tableView numberOfRowsInSection:indexPath.section] - 1)) {
                cell.backIV.image = [UIImage imageNamed:@"bottomBorder"];
                cell.lineView.hidden = YES;
            }else if (indexPath.row == 0) {
                cell.backIV.image = [UIImage imageNamed:@"topBorder"];
            }else {
                cell.backIV.image = [UIImage imageNamed:@"noBorder"];
            }
            
        }else {
            cell.backIV.image = [UIImage imageNamed:@"allBorder"];
            cell.lineView.hidden = YES;
            cell.nameLabel.text = @"设置";
        }
        
        return cell;
        
    }else {
        
        YYENoLoginTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nologinCell];
        
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
        
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.type isEqual:@0]) {
        return 46;
    }else if ([self.type isEqual:@1]) {
        return 46;
    }else {
        return 85;
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
    contentView.backgroundColor = GeneralBackColor;
    
    return contentView;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 20;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    YYELoginViewController *loginVC = [[YYELoginViewController alloc]init];
//    [self.navigationController pushViewController:loginVC animated:YES];

    if ([self.type isEqual:@0]) {
        
        if (indexPath.section == 0) {
            
            if (indexPath.row == 0) { // 课程收藏
                
                YYECourseCollectionViewController *collectionVC = [[YYECourseCollectionViewController alloc]init];
                [self.navigationController pushViewController:collectionVC animated:YES];
                
                
            }else if (indexPath.row == 1) { // 我的课程
                
                YYEMyCourseViewController *courseVC = [[YYEMyCourseViewController alloc]init];
                courseVC.title = @"我的课程";
                [self.navigationController pushViewController:courseVC animated:YES];
                
            }else if (indexPath.row == 2) { // 我的习题
                
                
                YYEMyTopicViewController *myTopic = [[YYEMyTopicViewController alloc]init];
                [self.navigationController pushViewController:myTopic animated:YES];
                
            }else if (indexPath.row == 3) { // 我的心得
                
                YYELatestNotesViewController *latestNoteVC = [[YYELatestNotesViewController alloc]init];
                latestNoteVC.mark = 1;
                latestNoteVC.title = @"我的心得";
                [self.navigationController pushViewController:latestNoteVC animated:YES];

                
            }else if (indexPath.row == 4) { // 我的钱包
                
                YYEWrongTopicViewController *vc = [[YYEWrongTopicViewController alloc]init];
                vc.title = @"错题本";
                [self.navigationController pushViewController:vc animated:YES];
                
            } else {
                
//                YYEWalletViewController *walletVC = [[YYEWalletViewController alloc]init];
//                [self.navigationController pushViewController:walletVC animated:YES];
            }
            
        }else {
            
            YYESettingViewController *settingVC = [[YYESettingViewController alloc]init];
            settingVC.delegate = self;
            [self.navigationController pushViewController:settingVC animated:YES];
            
        }
        
    }else if ([self.type isEqual:@1]) {
        
        if (indexPath.row == 0) { // 我的课程
            
            YYETeacCourseViewController *courseVC = [[YYETeacCourseViewController alloc]init];
            courseVC.title = @"我的课程";
            [self.navigationController pushViewController:courseVC animated:YES];
            
        }else if (indexPath.row == 1) { //  我的回答
            
            YYETeacMyAnswerViewController *vc = [[YYETeacMyAnswerViewController alloc]init];
            vc.title = @"我的问答";
            [self.navigationController pushViewController:vc animated:YES];
            
        }else if (indexPath.row == 2) { // 我的心得
            
            YYELatestNotesViewController *latestNoteVC = [[YYELatestNotesViewController alloc]init];
            latestNoteVC.mark = 1;
            latestNoteVC.title = @"我的心得";
            [self.navigationController pushViewController:latestNoteVC animated:YES];
            
        }else if (indexPath.row == 3) { // 数据统计
            
            
            
        }else if (indexPath.row == 4) { // 设置
            
            YYESettingViewController *settingVC = [[YYESettingViewController alloc]init];
            settingVC.delegate = self;
            [self.navigationController pushViewController:settingVC animated:YES];
            
        }
        
    }
    
}

#pragma mark -- NoLoginDelegate
- (void)Loginimmediately {
    
    YYELoginViewController *loginVC = [[YYELoginViewController alloc]init];
    [self.navigationController pushViewController:loginVC animated:YES];
    
}

// 账户管理
- (void)acountManaClicked {
    
    if ([LoginModel shareLogin].token.length == 0) {
        
//        YYELoginViewController *vc = [[YYELoginViewController alloc]init];
//        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    
    YYEAcountManaViewController *acountVC = [[YYEAcountManaViewController alloc]init];
    [self.navigationController pushViewController:acountVC animated:YES];
    
}

- (void)logOut {
    
    [self.tableView reloadData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
